package com.itcm.studyMongodb;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class MongoDBDemo {

    public static void main(String[] args) {
        // 建立连接
        MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");

        //选择数据库
        MongoDatabase testdb = mongoClient.getDatabase("testdb");

        // 选择表
        MongoCollection<Document> userConnection = testdb.getCollection("user");

        // 操作表 ： 查询
        userConnection.find().limit(10).forEach(document -> System.out.println(document.toJson()));

        // 关闭连接，释放资源
        mongoClient.close();
    }
}
